var app = getApp();
Page({

  data: {
    pageInfo: '',
    index: 0,
    index2: 0,
    array: [],
    date: '',
    qingjiashijian:'',
    liyou:'',
    date2:'',
    today:''
  },

  onLoad: function () {
    var that = this;
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }
    
    var date = new Date();
    var mingtian = date.getDate()
    console.log(date.getMonth())
    var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth()+1) + "-" + (mingtian < 10 ? ("0" + mingtian) : mingtian);

    that.setData({ date: todate});

    that.setData({ today: todate});

    var hours = []

    for (var i=date.getHours();i<24;i++)
    { 
      var xx = (i+1) +":00"
     hours.push(xx)
    }

    that.setData({ array: hours});
  
  },

  bindPickerChange: function(e) {
  
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })

    console.log(this.data.array[e.detail.value])

  },


  tijiao(){
      var liyou = this.data.liyou
      var kaishiriqi = this.data.date
      var qingjiashijian = this.data.qingjiashijian
      var kaishishijian = this.data.array[this.data.index]
      if(!kaishiriqi){
        wx.showModal({
          title: '温馨提示',
          content: '请选择开始日期',
          showCancel: false
        });
        return false
      }

      if(!kaishiriqi){
        wx.showModal({
          title: '温馨提示',
          content: '请选择开始日期',
          showCancel: false
        });
        return false
      }

      if(!kaishishijian){
        wx.showModal({
          title: '温馨提示',
          content: '请选择开始时间',
          showCancel: false
        });
        return false
      }

      
      
      if(!liyou){
        wx.showModal({
          title: '温馨提示',
          content: '请输入请假理由',
          showCancel: false
        });
        return false
      }
      
      if(qingjiashijian<=0){
        wx.showModal({
          title: '温馨提示',
          content: '请输入正确的请假天数',
          showCancel: false
        });
        return false
      }

      if(!liyou){
        wx.showModal({
          title: '温馨提示',
          content: '请输入请假理由',
          showCancel: false
        });
        return false
      }


   
    wx.request({
      url: app.apiUrl + '/api/index/anshiqingjia',
      data: {
        kaishiriqi:kaishiriqi,
        qingjiashijian:qingjiashijian,
        kaishishijian:kaishishijian,
        liyou:liyou,
        openid:wx.getStorageSync('openid'),
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {

        wx.showModal({
          title: '温馨提示',
          content: res.data,
          showCancel: false
        });
        
        setTimeout(function () {
          wx.navigateTo({
            url: '/page/my/index',
          });
        }, 2000)
    
      
      }
    })

  },
  bindKeyInput(e){
      console.log(e.detail.value)
      this.setData({ liyou: e.detail.value});
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)

    var hours = []
    var that = this
    var date = new Date();
    
    this.setData({
      date: e.detail.value
    })
    
    console.log(this.data.date)

    if(this.data.today == e.detail.value){

      console.log('选的今天')
      
    for (var i = date.getHours();i<24;i++)
    { 
      var xx = (i+1) +":00"
     hours.push(xx)
    }

    that.setData({ array: hours});
  
    }else{

      console.log('不是今天')
      
    for (var i=0;i<24;i++)
    { 
      var xx = (i+1) +":00"
     hours.push(xx)
    }

    that.setData({ array: hours});
  

    }


  },

  bindDateChange2: function(e) {

    this.setData({
      qingjiashijian: e.detail.value
    })


    
  },




  bindPickerChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value
    })
  },

})